﻿using System.ComponentModel;
using System.Globalization;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Lib.Common;

/// <summary>
/// 
/// </summary>
public static partial class Uilts {

    /// <summary>
    /// 服务器IP
    /// </summary>
    public static string IP {
        get {
            return Dns.GetHostAddresses(Dns.GetHostName()).Where(x => x.AddressFamily == AddressFamily.InterNetwork).Select(x => x.ToString())?.FirstOrDefault();
        }
    }
    /// <summary>
    /// 服务器IP
    /// </summary>
    public static string IPV6 {
        get {
            return Dns.GetHostAddresses(Dns.GetHostName()).Where(x => x.AddressFamily == AddressFamily.InterNetworkV6).Select(x => x.ToString())?.FirstOrDefault();
        }
    }
    /// <summary>
    /// 服务器IP（IPAddress）
    /// </summary>
    public static IPAddress InterNetwork {
        get {
            return Dns.GetHostAddresses(Dns.GetHostName()).Where(x => x.AddressFamily == AddressFamily.InterNetwork)?.FirstOrDefault();
        }
    }
    /// <summary>
    /// 服务器IP（IPAddress）
    /// </summary>
    public static IPAddress InterNetworkV6 {
        get {
            return Dns.GetHostAddresses(Dns.GetHostName()).Where(x => x.AddressFamily == AddressFamily.InterNetworkV6)?.FirstOrDefault();
        }
    }
    /// <summary>
    /// 根据日期获取星期几（中文）
    /// </summary>
    public static string Week(this DateTime Date) {
        Calendar Calendar = CultureInfo.InvariantCulture.Calendar;
        return Calendar.GetDayOfWeek(Date).ToString() switch {
            "Sunday" => "星期日",
            "Monday" => "星期一",
            "Tuesday" => "星期二",
            "Wednesday" => "星期三",
            "Thursday" => "星期四",
            "Friday" => "星期五",
            "Saturday" => "星期六",
            _ => null,
        };
    }
    /// <summary>
    /// 计算指定字符在源字符串的数量(忽略大小写)
    /// </summary>
    public static int Count(this string Source, string Char) {
        return Regex.Matches(Source, Regex.Escape(Char), RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture).Count;
    }
    /// <summary>
    /// 删除或替换字符串起头部的指定字符串(忽略大小写)
    /// </summary>
    public static string TrimStart(this string Source, string Char, string Value = null) {
        if (string.IsNullOrEmpty(Source)) {
            return Source;
        }
        if (Source.Length < Char.Length) {
            return Source;
        }
        try {
            if (Source.StartsWith(Char, StringComparison.OrdinalIgnoreCase)) {
                if (string.IsNullOrEmpty(Value)) {
                    return Source[Char.Length..];
                }
                return $"{Value}{Source[Char.Length..]}";
            }
            return Source;
        } catch {
            return Source;
        }
    }
    /// <summary>
    /// 删除或替换字符串尾部的指定字符串(忽略大小写)
    /// </summary>
    public static string TrimEnd(this string Source, string Char, string Value = null) {
        if (string.IsNullOrEmpty(Source)) {
            return Source;
        }
        if (Source.Length < Char.Length) {
            return Source;
        }
        try {
            if (Source.EndsWith(Char, StringComparison.OrdinalIgnoreCase)) {
                if(string.IsNullOrEmpty(Value)) {
                    return Source[..^Char.Length];
                }
                return $"{Source[..^Char.Length]}{Value}";
            }
            return Source;
        } catch {
            return Source;
        }
    }
    /// <summary>
    /// 删除从源字符串左边或右边开始查找到指定字符位置的字符串（包括指定字符）
    /// </summary>
    public static string Remove(this string Source, string Char, bool IsRight = false) {
        if (string.IsNullOrEmpty(Source)) {
            return Source;
        }
        int Length;
        if (IsRight) {
            Length = Source.LastIndexOf(Char);
            if (Length > -1) {
                return Source[..Length];
            }
        } else {
            Length = Source.IndexOf(Char);
            if (Length > -1) {
                return Source[(Length + 1)..];
            }
        }
        return Source;
    }
    /// <summary>
    /// 截取从源字符串左边或右边开始到指定字符位置的字符串（不包括指定字符）
    /// </summary>
    public static string Cut(this string Source, string Char, bool IsRight = false) {
        if (string.IsNullOrEmpty(Source)) {
            return Source;
        }
        int Length;
        if (IsRight) {
            Length = Source.LastIndexOf(Char);
            if (Length > -1) {
                return Source[(Length + 1)..];
            }
        } else {
            Length = Source.IndexOf(Char);
            if (Length > -1) {
                return Source[..Length];
            }
        }
        return Source;
    }
    /// <summary>
    /// 格式化时间 2019-12-08T00:28:39 转 2019-12-08 00:28:39
    /// </summary>
    public static string Format(string Date) {
        MatchCollection MM = RegexDate().Matches(Date);
        foreach (Match M in MM.Cast<Match>()) {
            Date = Date.Replace(M.Value, " " + M.Groups[2].Value);
        }
        return Date;
    }
    /// <summary>
    /// 获取 Enum 枚举的 Description 描述内容
    /// </summary>
    public static string GetDescription(this Enum e) {
        var Value = e.ToString();
        FieldInfo F = e.GetType().GetField(Value);
        object[] Result = F.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (Result.Length == 0) {
            return null;
        }
        return ((DescriptionAttribute)Result[0]).Description;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="Values"></param>
    /// <param name="Separator"></param>
    /// <returns></returns>
    public static string Join(this IEnumerable<int> Values, string Separator = ",") {
        if (Values != null) {
            return string.Join(Separator, Values);
        }
        return null;
    }
    /// <summary>
    /// 使用指定的分隔符连接字符串
    /// </summary>
    /// <param name="Values"></param>
    /// <param name="Separator"></param>
    /// <returns></returns>
    public static string Join(this IEnumerable<string> Values, string Separator = ",") {
        if (Values != null) {
            return string.Join(Separator, Values);
        }
        return null;
    }
    /// <summary>
    /// 使用指定的分隔符连接字符串
    /// </summary>
    /// <param name="Values"></param>
    /// <param name="Separator"></param>
    /// <returns></returns>
    public static string Join<T>(this IEnumerable<T> Values, string Separator = ",") {
        if (Values != null) {
            return string.Join<T>(Separator, Values);
        }
        return null;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [GeneratedRegex("(T)([0-9]?\\d:[0-5]?\\d:[0-5]?\\d)", RegexOptions.IgnoreCase | RegexOptions.Compiled, "zh-CN")]
    private static partial Regex RegexDate();
}
